// See the Electron documentation for details on how to use preload scripts:
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts
const { contextBridge, ipcRenderer } = require("electron");

contextBridge.exposeInMainWorld("globalConstants", {
    APP_Theme_Color: "#f9f9f9",
    APP_Border_Width: 0.5
})

contextBridge.exposeInMainWorld("MyAPI", {
    log: (message) => ipcRenderer.send('log-message', message),
    newLog: (callback) => ipcRenderer.on('new-log', callback),
    showColorPicker: (defaultColor, id, index) => ipcRenderer.send('showColorPicker', defaultColor, id, index),
    onColorPicked: (callback) => ipcRenderer.on('color-picked', callback),
    onColorPickError: (callback) => ipcRenderer.on('color-pick-error', callback),
    popupWindow: (fileName, width, height, fileData) => ipcRenderer.send('open-popup', fileName, width, height, fileData),
    closePopupWindow: () => ipcRenderer.send('close-popup'),
    setWindowSize: (width, height) => ipcRenderer.send('set-window-size', width, height),
    toBrowseFiles: () => ipcRenderer.invoke('browse-files'),
    chrome_version: process.versions.chrome,
    electron_version: process.versions.electron,
    node_version: process.versions.node,
    app_version: ipcRenderer.invoke('app_version'),
    readFiles: (file) => ipcRenderer.invoke('read-files', file),
    onDisplayFile: (callback) => ipcRenderer.on('display-file', callback),
    openLogin: () => ipcRenderer.send('open-log-window'),
    sendSignalSelect: (ch) => ipcRenderer.send('send-signal-select', ch),
    listenSignalSelect: (callback) => ipcRenderer.on('listen-signal-select', callback),
})

///端口接口
contextBridge.exposeInMainWorld('serialAPI', {
    listPorts: () => ipcRenderer.invoke("serial-list"),
    openPort: (path, name, baudRate) => ipcRenderer.send("open-port", path, name, baudRate),
    closePort: () => ipcRenderer.send("close-port"),
    sendData: (message) => ipcRenderer.send("send-data", message),
    sendDataWithList: (head, cmd, list, frency) => ipcRenderer.send("send-data-model-list", head, cmd, list, frency),
    listenPort: (callback) => ipcRenderer.on('listen-port', callback),
    currentPort: () => ipcRenderer.invoke('get-current-port'),
});

///地址接口
contextBridge.exposeInMainWorld('AddressAPI', {
    getAllAddresses: ipcRenderer.invoke('load-all-address'),
    updateAddress: (model) => ipcRenderer.invoke('update-address', model),
    findAddressById: (id) => ipcRenderer.invoke('find-address-id', id),
});

///通道接口
contextBridge.exposeInMainWorld('ChannellAPI', {
    getAllChannel: ipcRenderer.invoke('load-all-channel'),
    updateChannel: (ch) => ipcRenderer.invoke('update-channel', ch),
    linstenChannel: (callback) => ipcRenderer.on('linsten-channel', callback),
});

contextBridge.exposeInMainWorld('electronStoreAPI', {
    get: (key, defaultValue) => ipcRenderer.invoke('get-store-value', key, defaultValue),
    set: (key, value) => ipcRenderer.send('set-store-value', key, value),
});


